﻿package S010;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Group {
    private String ClassName;//班级名称
    private String ClassBlonger;//班级创建者
    private String InviteID;//邀请码
    
    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "root";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作 
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }
    
    public Group(String className,String classBlonger) {
        String range = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机邀请码取值范围
        Random random = new Random();
        StringBuilder tempID = null;
        do {
            tempID = new StringBuilder();//StringBuilder类临时邀请码，也可使用StringBuffer
            for ( int i = 0; i < 6; i++ ){//生成6位随机码
                tempID.append(range.charAt(random.nextInt(range.length())));
            }
        }while(existed(tempID.toString()));//邀请码已被使用，重新生成
        InviteID = tempID.toString();//toString将StringBuilder类型内容转存为String类型
        setClassName(className);
        setClassBlonger(classBlonger);
        setInviteID(InviteID);
        Connection conn = GetConnection();
        String sql = "insert into Class_t values(\""+InviteID+"\",\""+className+"\",\""+classBlonger+"\")";
        Statement sta = null;
        try {
            if(conn != null)
                sta = conn.createStatement();
            if(sta != null)
                sta.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(sta != null)    
                    sta.close();
                if(conn != null)
                    conn.close();
                sta = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        createMysqlClass(InviteID);
        System.out.println("班级创建成功:"+toString());
    }
    
    /*根据邀请码在数据库中创建对应的班级表，用于存储学生信息*/
    private void createMysqlClass(String InviteID) {
        Connection conn = GetConnection();
        String sql = "create table SClass_"+InviteID+"(StudentID char(11) primary key)";
        Statement sta = null;
        try {
            sta = conn.createStatement();
            sta.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(sta != null)
                    sta.close();
                if(conn != null)
                    conn.close();
                sta = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    private boolean existed(String ID) {//判断邀请码是否已使用
        Connection conn = GetConnection();
        String sql = "select InviteID from Class_t";
        ResultSet rs = null;
        Statement sta = null;
        List<String> IDlist = new ArrayList<>();//存放数据库中InvietID信息
        try {
            if(conn != null)
                sta = conn.createStatement();
            if(sta != null)
                rs = sta.executeQuery(sql);
            while(rs != null && rs.next()) {
                IDlist.add(rs.getString("InviteID"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)
                    rs.close();
                if(sta != null)    
                    sta.close();
                if(conn != null)
                    conn.close();
                rs = null;
                sta = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(IDlist.contains(ID))
            return true;
        return false;
    }
    
    public String getClassName() {
        return ClassName;
    }
    public void setClassName(String className) {
        ClassName = className;
    }
    public String getClassBlonger() {
        return ClassBlonger;
    }
    public void setClassBlonger(String classBlonger) {
        ClassBlonger = classBlonger;
    }
    public String getInviteID() {
        return InviteID;
    }
    public void setInviteID(String inviteID) {
        InviteID = inviteID;
    }

    @Override
    public String toString() {
        return "Group [ClassName=" + getClassName() + ", ClassBlonger=" + getClassBlonger() + ", InviteID=" + InviteID + "]";
    }
}
